package com.yiming.acimapplication;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.List;

/**
 * ACIM 工具类
 */
public class ACIMUtils {
    private static final String TAG = "ACIMUtils";

    /**
     * 计时器
     */
    public static class CountDown extends android.os.CountDownTimer {
        public CountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        public CountDown(long millisInFuture) {
            super(millisInFuture, 1000);
        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

        }
    }

    /**
     * 判断app是否正在前台运行
     *
     * @param context context
     * @return 在前台运行（true）
     */
    public static boolean isAppRunning(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = context.getPackageName();
        List<ActivityManager.RunningAppProcessInfo> runningProcessList = activityManager.getRunningAppProcesses();

        if (runningProcessList != null) {
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcessList) {
                if (processInfo.processName.equals(packageName) && processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    return true;
                }
            }
        }

        return false;
    }
}
